package com.csdj.t309springboot.controller;

import com.csdj.t309springboot.common.WebResult;
import com.csdj.t309springboot.common.WebResultMsgCode;
import com.csdj.t309springboot.utils.HttpRestUtil;
import com.csdj.t309springboot.vo.WxAccessRespVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@RestController
@RequestMapping("/wx")
public class WxController extends BaseController {
    @Value("${wx.appID}")
    private String wxAppId;
    @Value("${wx.appsecret}")
    private String wxappsecret;

    private Logger log = LoggerFactory.getLogger("wx");

    //页面授权接口
    private final static String OPEN_QRCODE_URL= "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=%s#wechat_redirect";

    //获取access_code接口
    private final static String WX_GET_ACCESS = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";


    private final static String LOGIN_CALLBACK="http://j4867o7433.zicp.vip/api/wx/login/callback";
    @GetMapping("/token")
    public String domainCheck(@RequestParam("signature")String signature,
                              @RequestParam("timestamp")Long timestamp,
                              @RequestParam("nonce")String nonce,
                              @RequestParam("echostr")String echostr){
        log.info("signature:{} timestamp:{},nonce:{},echostr:{}",signature,timestamp,nonce,echostr);
        return echostr;
    }

    /**
     * 授权回调接口
     * @param code
     * @param state
     * @param request
     * @return
     */
    @RequestMapping("/login/callback")
    public String loginCallback(String code,String state,HttpServletRequest request){
        log.info("code:{} state:{}",code,state);
        //获取access_code
        String url = String.format(WX_GET_ACCESS,wxAppId,wxappsecret,code);
        String resp = HttpRestUtil.get(url, String.class);
        ObjectMapper om = new ObjectMapper();
        WxAccessRespVo accessResp = null;
        try {
            accessResp = om.readValue(resp, WxAccessRespVo.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        if(accessResp != null && StringUtils.hasText(accessResp.getAccess_token())){
            //获取openid
            System.out.println(accessResp.getOpenid());
        }
        return "success";
    }

    @GetMapping("/login/qrcode")
    public WebResult getLoginUrl(String state) throws UnsupportedEncodingException {
        String callbackUrl = URLEncoder.encode(LOGIN_CALLBACK,"GBK"); //进行编码
        String qrcode = String.format(OPEN_QRCODE_URL,wxAppId,callbackUrl,state);
        return resp(WebResultMsgCode.SUCCESS,qrcode);
    }
}
